#!/bin/bash

VERSION=$(git describe)
TMP_DIR=`mktemp -d`
if [ $? -eq 0 ]; then
    # Make sure coreutils is installed.
    # if not, these terrible things may happen:
    # https://www.letscontrolit.com/forum/viewtopic.php?f=6&t=8542
    echo "Created ${TMP_DIR}"
else
    echo "Could not create tmp dir, mktemp -d failed"
    exit 1
fi

TMP_DIST=`echo "${TMP_DIR}/ESPEasy_collect_dist"`
mkdir -p ${TMP_DIST}
if [ $? -eq 0 ]; then
    echo "Created ${TMP_DIST}"
else
    echo "Could not create tmp dir: ${TMP_DIST}"
    exit 1
fi

CURPATH=`pwd`
BUILD_LOG=`echo "${TMP_DIST}/buildlog.txt"`

# PIO 3.x :
# BINARY_PATH=".pioenvs"

# PIO 4.0 and newer:
BINARY_PATH=".pio/build"

while getopts d: option
do
case "${option}"
in
d) VERSION=${OPTARG};;
esac
done


if [ ! -d ${TMP_DIST} ]; then
  # Could not create temp dir, so exit
  exit 1
fi

echo "### Creating zip archives"

#Naming convention:
# ESP_Easy_[github version]_[plugin set]_[chip type]_[flash memory].bin

if [ -d "build_output/reject" ]; then
  zip -qq ${CURPATH}/ESPEasy_ELF_files_$VERSION.zip build_output/reject/*
fi

zip -qq ${CURPATH}/ESPEasy_ELF_files_$VERSION.zip build_output/debug/*
echo "### Created ESPEasy_ELF_files_$VERSION.zip"


mkdir -p ${TMP_DIST}
cp -r dist/* ${TMP_DIST}/

if [ -d "docs/build" ]; then
  # Docs have been created
  zip -r -qq ${CURPATH}/ESPEasy_docs_$VERSION.zip docs/build/*
  echo "### Created ESPEasy_docs_$VERSION.zip"
fi

#create a source structure that is the same as the original ESPEasy project (and works with the howto on the wiki)
#rm -rf dist/Source 2>/dev/null

mkdir -p ${TMP_DIST}/source
cp -r lib ${TMP_DIST}/source/
cp -r src ${TMP_DIST}/source/
cp -r misc ${TMP_DIST}/source/
cp -r static ${TMP_DIST}/source/
cp -r tools ${TMP_DIST}/source/
cp platformio*.ini ${TMP_DIST}/source/
cp *.txt ${TMP_DIST}/source/
cp *.csv ${TMP_DIST}/source/
cp README* ${TMP_DIST}/source/

cp -r build_output/bin/* ${TMP_DIST}/bin
rm -f ${TMP_DIST}/bin/*ESP32*

cd ${TMP_DIST}

if [ "$(ls -A ${TMP_DIST}/bin/)"  ]; then
  echo
  zip -qq ${CURPATH}/ESPEasy_ESP82xx_$VERSION.zip -r .
  echo "### Created ESPEasy_ESP82xx_$VERSION.zip"
fi

cd ${CURPATH}
rm -f ${TMP_DIST}/bin/*
cp -r build_output/bin/*ESP32* ${TMP_DIST}/bin

cd ${TMP_DIST}

if [ "$(ls -A ${TMP_DIST}/bin/)"  ]; then
  echo
  zip -qq ${CURPATH}/ESPEasy_ESP32_$VERSION.zip -r .
  echo "### Created ESPEasy_ESP32_$VERSION.zip"
fi

rm -Rf ${TMP_DIST}/* 2>/dev/null
rmdir ${TMP_DIST}
